package com.w1.security.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.w1.security.domain.LoginUser;
import com.w1.security.domain.ResponseResult;
import com.w1.security.domain.User;
import com.w1.security.domain.Vo.FindLikePageVo;
import com.w1.security.domain.Vo.PHBListVo;
import com.w1.security.querymodel.QueryPageInfo;
import com.w1.security.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author mingyifan
 * @date 2022-03-07 16:51
 */
@RestController
public class gameController {
    @Autowired
    private IUserService iUserService;

    /**
     *  胜利+1后触发
     *  @param
     *  @return com.w1.security.domain.ResponseResult
     *  mingyifan
     *  2022/3/7 16:52
     */
    @RequestMapping("/vitory")
    public ResponseResult vitory(){
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String id = loginUser.getUser().getId();
        int i = iUserService.updateUserWinGames(id);
        if (i==1){
            return new ResponseResult(200,"胜场+1成功");
        }
        return new ResponseResult(200,"失败",i);
    }
    /**
     *   获取排行榜，胜场倒序
     *  @param queryPageInfo 分页参数
     *  @return com.w1.security.domain.ResponseResult
     *  mingyifan
     *  2022/3/8 16:09
     */
    @RequestMapping("/getPHBList")
    public ResponseResult getPHBList(@RequestBody QueryPageInfo queryPageInfo){
        Page<User> userPage = new Page<>(queryPageInfo.getPageIndex(), queryPageInfo.getPageSize());
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("win_games");
        wrapper.eq("is_join_rank_list",0);
        //拿数据库
        Page<User> userPage1 = iUserService.getBaseMapper().selectPage(userPage, wrapper);
        FindLikePageVo trans = FindLikePageVo.trans(userPage1, PHBListVo.trans(userPage1.getRecords()));
        return new ResponseResult(200,"获取排行榜成功",trans);
    }
}
